//
//  TimeTool.m
//  TrainTree
//
//  Created by wanghui on 2017/10/4.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "TimeTool.h"

@implementation TimeTool
+ (NSString *)convertTimeFromSeconds:(NSString *)second
{
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",([second integerValue] % 3600)/60];
    NSString *str_second = [NSString stringWithFormat:@"%02ld",[second integerValue] % 60];
     return  [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
}
+(NSString *)getCurrentTimes
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    //现在时间,你可以输出来看下是什么格式
    NSDate *datenow = [NSDate date];
    //----------将nsdate按formatter格式转成nsstring
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;

}

+ (NSString *)getTimeIntervalWithStartTime:(NSString *)startTime endTime:(NSString *)endTime;
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    // 开始时间
    NSDate *startDate = [fmt dateFromString:startTime];
    // 结束时间
    NSDate *endDate = [fmt dateFromString:endTime];

    // 获得NSCalendar
    NSCalendar *calendar = nil;
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    } else {
        calendar = [NSCalendar currentCalendar];
    }
    // 获得日期之间的间隔
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *cmps = [calendar components:unit fromDate:startDate toDate:endDate options:0];

    return [NSString stringWithFormat:@"%ld", (long)cmps.second];
}
@end
